import request from '@/utils/request'
import type { PmTeamVO, PmTeamQuery, PmTeamForm } from './types'

// 查询团队管理列表
export function getTeamList(query: PmTeamQuery) {
  return request({
    url: '/pm/team/list',
    method: 'get',
    params: query
  })
}

// 查询团队列表（别名，支持无参数调用）
export function listTeam(query?: PmTeamQuery) {
  return getTeamList(query || {})
}

// 查询团队管理详细
export function getTeam(teamId: string | number) {
  return request({
    url: '/pm/team/' + teamId,
    method: 'get'
  })
}

// 新增团队管理
export function addTeam(data: PmTeamForm) {
  return request({
    url: '/pm/team',
    method: 'post',
    data: data
  })
}

// 修改团队管理
export function updateTeam(data: PmTeamForm) {
  return request({
    url: '/pm/team',
    method: 'put',
    data: data
  })
}

// 删除团队管理
export function delTeam(teamId: string | number | Array<string | number>) {
  return request({
    url: '/pm/team/' + teamId,
    method: 'delete'
  })
}

// 根据团队编码查询团队
export function getTeamByCode(teamCode: string) {
  return request({
    url: '/pm/team/code/' + teamCode,
    method: 'get'
  })
}

// 根据团队负责人查询团队列表
export function getTeamByLeader(teamLeaderId: string | number) {
  return request({
    url: '/pm/team/leader/' + teamLeaderId,
    method: 'get'
  })
}

// 根据团队类型查询团队列表
export function getTeamByType(teamType: string) {
  return request({
    url: '/pm/team/type/' + teamType,
    method: 'get'
  })
}

// 检查团队编码是否唯一
export function checkTeamCodeUnique(teamCode: string, teamId?: string | number) {
  return request({
    url: '/pm/team/checkTeamCodeUnique',
    method: 'get',
    params: {
      teamCode,
      teamId
    }
  })
}

// 检查团队名称是否唯一
export function checkTeamNameUnique(teamName: string, teamId?: string | number) {
  return request({
    url: '/pm/team/checkTeamNameUnique',
    method: 'get',
    params: {
      teamName,
      teamId
    }
  })
}

// 导出团队管理
export function exportTeam(query: PmTeamQuery) {
  return request({
    url: '/pm/team/export',
    method: 'post',
    data: query
  })
}

// ==================== 团队成员管理 ====================

// 查询团队成员列表
export function listTeamMember(query: any) {
  return request({
    url: '/pm/team/member/list',
    method: 'get',
    params: query
  })
}

// 查询团队成员详细
export function getTeamMember(memberId: string | number) {
  return request({
    url: '/pm/team/member/' + memberId,
    method: 'get'
  })
}

// 新增团队成员
export function addTeamMember(data: any) {
  return request({
    url: '/pm/team/member',
    method: 'post',
    data: data
  })
}

// 修改团队成员
export function updateTeamMember(data: any) {
  return request({
    url: '/pm/team/member',
    method: 'put',
    data: data
  })
}

// 删除团队成员
export function delTeamMember(memberIds: string | number | Array<string | number>) {
  return request({
    url: '/pm/team/member/' + memberIds,
    method: 'delete'
  })
}

// 批量添加团队成员
export function batchAddTeamMember(data: any) {
  return request({
    url: '/pm/team/member/batch',
    method: 'post',
    data: data
  })
}

// 团队成员管理相关接口
export * from './team-member'

// ==================== 团队角色管理 ====================

// 查询团队角色列表
export function listTeamRole(query: any) {
  return request({
    url: '/pm/team/role/list',
    method: 'get',
    params: query
  })
}

// 查询团队角色详细
export function getTeamRole(roleId: string | number) {
  return request({
    url: '/pm/team/role/' + roleId,
    method: 'get'
  })
}

// 新增团队角色
export function addTeamRole(data: any) {
  return request({
    url: '/pm/team/role',
    method: 'post',
    data: data
  })
}

// 修改团队角色
export function updateTeamRole(data: any) {
  return request({
    url: '/pm/team/role',
    method: 'put',
    data: data
  })
}

// 删除团队角色
export function delTeamRole(roleIds: string | number | Array<string | number>) {
  return request({
    url: '/pm/team/role/' + roleIds,
    method: 'delete'
  })
}

// 分配角色权限
export function assignRolePermissions(roleId: string | number, permissionIds: string[] | number[]) {
  return request({
    url: '/pm/team/role/assignPermissions',
    method: 'post',
    data: {
      roleId,
      permissionIds
    }
  })
}

// 获取角色权限
export function getRolePermissions(roleId: string | number) {
  return request({
    url: '/pm/team/role/permissions/' + roleId,
    method: 'get'
  })
}

// 获取权限树
export function getPermissionTree() {
  return request({
    url: '/pm/team/permission/tree',
    method: 'get'
  })
}

// ==================== 团队权限管理 ====================

// 查询团队权限列表
export function listTeamPermission(query: any) {
  return request({
    url: '/pm/team/permission/list',
    method: 'get',
    params: query
  })
}

// 查询团队权限详细
export function getTeamPermission(permissionId: string | number) {
  return request({
    url: '/pm/team/permission/' + permissionId,
    method: 'get'
  })
}

// 新增团队权限
export function addTeamPermission(data: any) {
  return request({
    url: '/pm/team/permission',
    method: 'post',
    data: data
  })
}

// 修改团队权限
export function updateTeamPermission(data: any) {
  return request({
    url: '/pm/team/permission',
    method: 'put',
    data: data
  })
}

// 删除团队权限
export function delTeamPermission(permissionIds: string | number | Array<string | number>) {
  return request({
    url: '/pm/team/permission/' + permissionIds,
    method: 'delete'
  })
}